<?php 

    $allLinks = array();

    function saveContent($content, $fileName)
    {            
        if(!$file = fopen(EditFileName($fileName), 'w'))
        {
            echo "Work was stopped! Not all of the resources produced!";
        }
        else        
        {
           fputs($file, $content);
           fclose($file);
        }                          
    }     

    function EditFileName($fileName)
    {
        return CONTENT_FOLDER . str_replace(array("/", "\"", ":", "?", "*", "\\", "|", "<", ">", "=", "$", "@", "%", "^", "!", "-"), "_", $fileName) . FILE_EXTENSION;
    }
    
    function parseURLs($matches, $file)
    {     
        $newLinks = array();
        
        for($i = 0; $i < count($matches); $i++)
        { 
            $currLink = UrlManager::ParseUrl($matches[$i]);              
            if((!in_array($currLink, $allLinks))&&($currLink != ""))
            {
                $allLinks[count($allLinks)] = $currLink;
                fputs($file, $currLink . "\n");
                $newLinks = array_merge($newLinks, getContent($currLink, $file, FALSE));
            }                       
        }
                        
        return $newLinks;                
    }   
    
    function getContent($url, $file, $isFirstPage)
    {        
        $client = UrlManager::GetClient($url);      
        $content = UrlManager::GetMainBlockContent(curl_exec($client));
        if($content != "")
        {               
            saveContent($content, $url);
        }
        preg_match_all(PATTERN, $content, $matches, PREG_PATTERN_ORDER);
        
        return $isFirstPage ? $matches[1] : UrlManager::EditUrls($matches, $url);
    }    
?>